#include "stdafx.h"
#include "CMSGFitStreamCon.h"
#include "MainMenuPainter.h"
#include "IResCfg.h"
#include "CMSGInforCon.h"

CMSGFitStreamCon::CMSGFitStreamCon(HWND hWnd):CMSGInforCon(hWnd)
{
m_nIndexPos=0;
m_count=0;
}

CMSGFitStreamCon::~CMSGFitStreamCon(void)
{
	m_pListBox->StopSearch();
}

BOOL CMSGFitStreamCon::InitControls(DWORD dwData)
{
	//message box
	LANGUAGE_DECLARE;
	FONT_DECLARE;

	HBITMAP hBitmap = NULL;

	CIResCfg ResCon;
	if(!ResCon.Init(L"MSGFitStream.xml"))
	{
		//g_App.ShowMsgBox(CMD_MSGBOX_OK,L"",LAN_GETTEXT(Msg_ResError));
		return FALSE;
	}

	m_nAniType  = ResCon.GetAniType();
	m_nExitAniType=ResCon.GetExitAniType();
	m_nAniSpeed = ResCon.GetAniSpeed();
	SetRect(ResCon.GetRect());
	BOOL bFromDll = ResCon.IsResFromDll();
	BtnParam btnInfo;

	hBitmap = CUtil::LoadBitmapEx(ResCon.GetImage(),bFromDll);
    this->SetMaskBmp(hBitmap);

	BatTimePicParam picInfo;
	ResCon.GetBatTimePicParam(picInfo,L"Pic_Bkgnd");
	CPicture *pPic = (CPicture*)AddControl(new CPicture(SWS_STATIC, L"",picInfo.m_rc, CMD_MSGBOX_PIC));
	ResCon.SetPicParam(pPic,picInfo);

	TextParam txtInfo;
	CText* pText=NULL; 


	//{
	ResCon.GetTextParam(txtInfo,L"Title");
	m_pTitle = (CText*)AddControl(new CText(SWS_STATIC,LAN_GETTEXT(MSG_HS_FITSTREAM_TITLE),txtInfo.m_rc, CMD_MSG_FITSTREAM_TEXT));
	ResCon.SetTextParam(m_pTitle,txtInfo);
	//}
	CBmpBtn* pBtn = NULL;

	ResCon.GetBtnParam(btnInfo,L"Btn_No");
	pBtn = (CBmpBtn*)AddControl(new CBmpBtn(SBS_RECT,LAN_GETTEXT(TXT_CANCLE),btnInfo.m_rc, CMD_MSG_FITSTREAM_CANCLE));
	ResCon.SetBtnParam(pBtn,btnInfo);

	ResCon.GetBtnParam(btnInfo,L"Btn_Yes");
	m_pOK = (CBmpBtn*)AddControl(new CBmpBtn(SBS_RECT,LAN_GETTEXT(MSG_OK),btnInfo.m_rc, CMD_MSG_FITSTREAM_OK));
	ResCon.SetBtnParam(m_pOK,btnInfo);
    m_pOK->Enable(FALSE);


	ResCon.GetTextParam(txtInfo,L"Text_InsertUsb");
	m_pInsertUsb = (CText*)AddControl(new CText(SWS_STATIC,L"Insert USB Device now to load videos",txtInfo.m_rc, CMD_MSG_FITSTREAM_INSERTUSB));
	ResCon.SetTextParam(m_pInsertUsb,txtInfo);
	m_pInsertUsb->Show(false);






	ResCon.GetTextParam(txtInfo,L"FitStream_FileName");
	m_pFileName = (CText*)AddControl(new CText(SWS_STATIC,LAN_GETTEXT(MSG_HS_FITSTREAM_NAME),txtInfo.m_rc, CMD_MSG_FITSTREAM_FILENAME));
	ResCon.SetTextParam(m_pFileName,txtInfo);
	m_pFileName->Show(false);

	ResCon.GetTextParam(txtInfo,L"FitStream_Sel");
	m_pFileSel = (CText*)AddControl(new CText(SWS_STATIC,LAN_GETTEXT(MSG_HS_FITSTREAM_SEL),txtInfo.m_rc, CMD_MSG_FITSTREAM_FILESEL));
	ResCon.SetTextParam(m_pFileSel,txtInfo);
	m_pFileSel->Show(false);




	ResCon.GetBtnParam(btnInfo,L"FitStreamList_BTN_DOWN");
	m_pBtnDown = (CBmpBtn*)AddControl(new CBmpBtn(SBS_RECT,L"",btnInfo.m_rc, CMD_MSG_FITSTREAM_LISTBOX_DOWN));
	ResCon.SetBtnParam(m_pBtnDown,btnInfo);
    m_pBtnDown->Show(false);


	ResCon.GetBtnParam(btnInfo,L"FitStreamList_BTN_UP");
	m_pBtnUp = (CBmpBtn*)AddControl(new CBmpBtn(SBS_RECT,L"",btnInfo.m_rc, CMD_MSG_FITSTREAM_LISTBOX_UP));
	ResCon.SetBtnParam(m_pBtnUp,btnInfo);
	m_pBtnUp->Show(false);



	ListParam listInfo;
	ResCon.GetListParam(listInfo,L"Video_List");
	m_pListBox = (CCheckListBox*)AddControl(new CCheckListBox(SBS_RECT, L"",listInfo.m_rc, CMD_MSG_FITSTREAM_LISTBOX));

	m_pListBox->SetExtendedStyle(listInfo.m_dwStyle);
	m_pListBox->SetAlign(listInfo.m_dwAlign);
	m_pListBox->SetTxtNormal(listInfo.m_clrTxtNormal);
	m_pListBox->SetTxtActive(listInfo.m_clrTxtActive);
	m_pListBox->SetFont(FNT_GETFONT(listInfo.m_strFont));
	m_pListBox->SetSelItemBmp(CUtil::LoadBitmapEx(listInfo.m_strSelItem,bFromDll));
	m_pListBox->SetLineStartClr(listInfo.m_clrLineStart);
	m_pListBox->SetLineEndClr(listInfo.m_clrLineEnd);

	if(listInfo.m_dwStyle & LISTBOXEX_STYLE_CHECKBOX)
	{
		hBitmap = CUtil::LoadBitmapEx(listInfo.m_strImg,bFromDll);
		CImageList* pImgList = new CImageList();
		pImgList->Create(listInfo.m_szIcon.cx,listInfo.m_szIcon.cy,listInfo.m_bTranparent);
		pImgList->Add(hBitmap,listInfo.m_clrTranparent);
		m_pListBox->SetImageListCheck(pImgList);
		m_pListBox->SetCheckPointPos(CPoint(450,20));
		m_pListBox->SetContainer(this);
	}
	m_pListBox->Show(false);


	ResCon.GetTextParam(txtInfo,L"Text_NoFile");
	m_pNoFile = (CText*)AddControl(new CText(SWS_STATIC,LAN_GETTEXT(MSG_HS_FITSTREAM_NOFILETITLE),txtInfo.m_rc, CMD_MSG_FITSTREAM_NOFILE));
	ResCon.SetTextParam(m_pNoFile,txtInfo);
	m_pNoFile->Show(false);

	ResCon.GetTextParam(txtInfo,L"Text_NoFileWaring");
	m_pNoFileWarning = (CText*)AddControl(new CText(SWS_STATIC,LAN_GETTEXT(MSG_HS_FITSTREAM_NOFILEWARNING),txtInfo.m_rc, CMD_MSG_FITSTREAM_NOFILEWARNING));
	ResCon.SetTextParam(m_pNoFileWarning,txtInfo);
    m_pNoFileWarning->Show(false);


	ResCon.GetBatTimePicParam(picInfo,L"Icon_USB");
	m_picUSB = (CPicture*)AddControl(new CPicture(SWS_STATIC, L"",picInfo.m_rc, CMD_MSG_FITSTREAM_PRO));
	ResCon.SetPicParam(m_picUSB,picInfo);
    m_picUSB->Show(false);
	ResCon.GetTextParam(txtInfo,L"Title_LOAD");
	m_Load = (CText*)AddControl(new CText(SWS_STATIC,L"Loading",txtInfo.m_rc, CMD_MSG_FITSTREAM_TEXT));
	ResCon.SetTextParam(m_Load,txtInfo);
	m_Load->Show(false);








	// m_picUSB->Show(false);


	ProgressParam proInfo;	

	ResCon.GetProgreeParam(proInfo,L"ClubSet_Loading_PRO");
	m_pProgress = (CProgress*)AddControl(new CProgress(SBS_RECT, L"", proInfo.m_rc, CMD_MSGBOX_PIC));
	ResCon.SetProgressParam(m_pProgress,proInfo);

	m_pProgress->SetRange(0,15);

	m_pProgress->SetPos(0);
	m_pProgress->Show(false);

	m_veFilter.push_back(L"mp4");
	m_veFilter.push_back(L"wmv");
	m_veFilter.push_back(L"avi");
	m_veFilter.push_back(L"asf");
	m_pListBox->SetCurDir(CUtil::GetStorageCardName());
	m_pListBox->SetExtName(&m_veFilter);
	switch(dwData)
	{
	case 0:
		{
            m_pTitle->SetCaption(LAN_GETTEXT(MSG_HS_FITSTREAM));
			m_pInsertUsb->Show(true);
			m_picUSB->Show(true);

		}
		break;
	case 1:
		{

		
			m_Load->Show(true);
			m_pProgress->Show(true);
			
			m_picUSB->Show(true);
 
            m_pTitle->SetCaption(LAN_GETTEXT(MSG_HS_FITSTREAM));
			m_pListBox->DeleteAllItems();
		
             
			m_pListBox->RunSearch();
			m_pOK->Enable(TRUE);
		}
		break;
	case 2:
		{

			m_pNoFile->Show(true);
			m_pNoFileWarning->Show(true);
			m_picUSB->Show(true);
			 m_pTitle->SetCaption(LAN_GETTEXT(MSG_HS_FITSTREAM_TITLE));

		}
		break;
	}
	


  
	
	SetPainter(new CMainMenuPainter());
	return true;
}
void  CMSGFitStreamCon::HaveFile()
{
	LANGUAGE_DECLARE;
	m_Load->Show(false);
	m_pProgress->Show(false);
	m_picUSB->Show(false);

	m_pListBox->Show(true);
	m_pBtnDown->Show(true);
	m_pBtnUp->Show(true);
	m_pFileName->Show(true);
	m_pFileSel->Show(true);
	m_pOK->Enable(TRUE);
    m_pTitle->SetCaption(LAN_GETTEXT(MSG_HS_FITSTREAM_TITLE));
	CPicture *pPic =(CPicture *)ControlFromID(CMD_MSGBOX_PIC);
	m_pListBox->SetSelItem(-1);


	InvalidateRect(m_hWnd,pPic->GetRect(),FALSE);

}
void  CMSGFitStreamCon::PocessNoFileWarning()
{

	LANGUAGE_DECLARE;
	m_Load->Show(false);
	m_pProgress->Show(false);

	m_pListBox->Show(false);
	m_pBtnDown->Show(false);
	m_pBtnUp->Show(false);
	m_pFileName->Show(false);
	m_pFileSel->Show(false);
	m_pInsertUsb->Show(false);

	m_pNoFile->Show(true);
	m_pNoFileWarning->Show(true);
	m_picUSB->Show(true);
	m_pOK->Enable(FALSE);
	m_pTitle->SetCaption(LAN_GETTEXT(MSG_HS_FITSTREAM_TITLE));
	CPicture *pPic =(CPicture *)ControlFromID(CMD_MSGBOX_PIC);


	InvalidateRect(m_hWnd,pPic->GetRect(),FALSE);

}
void  CMSGFitStreamCon::PocessUsbOutEvent()
{
   	LANGUAGE_DECLARE;
	m_pInsertUsb->Show(true);
	m_picUSB->Show(true);

	m_Load->Show(false);
	m_pProgress->Show(false);

	m_pNoFile->Show(false);
	m_pNoFileWarning->Show(false);


	m_pListBox->Show(false);
	m_pBtnDown->Show(false);
	m_pBtnUp->Show(false);
	m_pFileName->Show(false);
	m_pFileSel->Show(false);

	m_pListBox->StopSearch();
	m_pListBox->DeleteAllItems();
    m_pOK->Enable(FALSE);
	m_pTitle->SetCaption(LAN_GETTEXT(MSG_HS_FITSTREAM));
	CPicture *pPic =(CPicture *)ControlFromID(CMD_MSGBOX_PIC);


	InvalidateRect(m_hWnd,pPic->GetRect(),FALSE);

}

void  CMSGFitStreamCon::PocessTimeEvent()
{
	LANGUAGE_DECLARE;
	m_picUSB->Show(true);
	if (m_pProgress)
	{

		if (m_pProgress->GetPos()>=15)
		{  
			m_pProgress->SetPos(0);
		}
		m_pProgress->SetPos(m_pProgress->GetPos()+1);
		m_count++;
		if (m_count>40)
		{
			KillTimer(m_hWnd,15);
            PostMessage(m_hWnd,WM_SEARCH_END,0,0);
			m_count=0;
		}
	}
	InvalidateRect(m_hWnd,m_pProgress->GetRect(),FALSE);
}
void  CMSGFitStreamCon::PocessUsbInsertEvent()
{
   	LANGUAGE_DECLARE;

	m_Load->Show(true);
	m_pProgress->Show(true);

	m_pInsertUsb->Show(false);
	m_pNoFileWarning->Show(false);
	m_pNoFile->Show(false);

	m_picUSB->Show(true);



    m_pListBox->DeleteAllItems();
  
	//m_pListBox->RunSearch();
   	m_pTitle->SetCaption(LAN_GETTEXT(MSG_HS_FITSTREAM));
	CPicture *pPic =(CPicture *)ControlFromID(CMD_MSGBOX_PIC);
	m_pProgress->SetPos(0);
	SetTimer(m_hWnd,15,500,NULL);
	
    InvalidateRect(m_hWnd,pPic->GetRect(),FALSE);
}

void  CMSGFitStreamCon::OnCommand(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
	switch (wParam)
	{
	case CMD_MSG_FITSTREAM_LISTBOX_UP:
		m_nIndexPos--;
		if(m_nIndexPos < 0)
		{
			m_nIndexPos = 0;
		}
	
		m_pListBox->SetCurPage(m_nIndexPos);
		break;

	case CMD_MSG_FITSTREAM_LISTBOX_DOWN:
		m_nIndexPos++;
		if(m_nIndexPos >= m_pListBox->GetTotalPage())
		{
			m_nIndexPos = m_pListBox->GetTotalPage()-1;
		}

		m_pListBox->SetCurPage(m_nIndexPos);
		break;

	

	case CMD_MSG_FITSTREAM_LISTBOX:
		{
			//{
			
		/*	m_pProgress->Show(true);
			m_picUSB->Show(true);
			m_Load->Show(true);
			m_pListBox->Show(false);
			m_pTitle->SetCaption(L"Trainer Video");
			SetTimer(hWnd,15,500,NULL);*/
		
		
		}
        break;
	default:
		break;
	}

   InvalidateRect(m_hWnd,NULL,FALSE);
}